package com.qf.dao.impl;


import com.qf.dao.UserDao;
import com.qf.pojo.TbUser;
import com.qf.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.Date;

/**
 * Create by 周志豪 on 2022/8/16  19:14
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */

public class UserDaoImpl implements UserDao {

    @Override
    public int addUser(TbUser user) {
        QueryRunner runner = DbUtils.getRunner();
        String sql = "INSERT INTO tb_user (username,password,email,gender,code) VALUES(?,?,?,?,?)";
        try {
            int count = runner.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getGender(), user.getCode());
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TbUser findByUserName(String name) {
        QueryRunner runner = DbUtils.getRunner();
        String sql = "select * from tb_user where username = ?";
        try {
            TbUser user = runner.query(sql, new BeanHandler<>(TbUser.class), name);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateLoginTime(Long userId) {
        QueryRunner runner = DbUtils.getRunner();
        String sql = "update tb_user set lastlogintime=currentlogintime,currentlogintime=? where id =?";
        try {
            int count = runner.update(sql, new Date(),userId);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
